package com.cskaoyan.app.controller;

import com.cskaoyan.app.bean.bo.GoodsAddBO;
import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.*;
import com.cskaoyan.app.bean.vo.CreateGoodsVo;
import com.cskaoyan.app.bean.vo.CreategoodsCatAndBrandVo;
import com.cskaoyan.app.bean.vo.GoodsDetailVo;

import com.cskaoyan.app.service.AdminGoodsCatAndBrandService;
import com.cskaoyan.app.service.AdminGoodsDetailService;
import com.cskaoyan.app.service.AdminGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/goods")
public class AdminGoodsController {
    @Autowired
    AdminGoodsService adminGoodsService;
    @Autowired
    AdminGoodsCatAndBrandService adminGoodsCatAndBrandService;
    @Autowired
    AdminGoodsDetailService adminGoodsDetailService;


    //http://182.92.235.201:8083/admin/goods/list?page=1&limit=20&
    // goodsSn=1000&name=nihao&sort=add_time&order=desc&
    // goodsId=1181055
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public BaseRespVo list(BasePageInfo basePageInfo, String goodsSn, String name, Integer goodsId) {
        // 开启分页
        Integer page = basePageInfo.getPage();
        Integer limit = basePageInfo.getLimit();
        if(limit!=0){
            PageHelper.startPage(page, limit);
        }
        List<MarketGoods> goods = adminGoodsService.listPageGoods(basePageInfo, goodsId, goodsSn, name);
        PageInfo<MarketGoods> goodsPageInfo = new PageInfo<>(goods);
        CommonData<MarketGoods> data = CommonData.data(goodsPageInfo);
        return BaseRespVo.ok(data);
    }

    @RequestMapping(value = "catAndBrand", method = RequestMethod.GET)
    public BaseRespVo catAndBrand() {
        CreategoodsCatAndBrandVo catAndBrandVo = new CreategoodsCatAndBrandVo();
        List<CreategoodsCatAndBrandVo.CategoryListEntity> categoryList = adminGoodsCatAndBrandService.getListCategoryListEntity();
        List<CreategoodsCatAndBrandVo.BrandListEntity> brandList = adminGoodsCatAndBrandService.getListBrandListEntity();
        //  private List<CategoryListEntity> categoryList;
        //  private List<BrandListEntity> brandList;
        catAndBrandVo.setBrandList(brandList);
        catAndBrandVo.setCategoryList(categoryList);
        return BaseRespVo.ok(catAndBrandVo);
    }

    @RequestMapping(value = "detail", method = RequestMethod.GET)
    public BaseRespVo detail(Integer id) {
        GoodsDetailVo date = adminGoodsDetailService.getGoodsDetailVo(id);
        return BaseRespVo.ok(date);
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody CreateGoodsVo createGoodsVo) {
        adminGoodsService.updateGoods(createGoodsVo);
        return BaseRespVo.ok();
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketGoods marketGoods) {

        adminGoodsService.deleteGoods(marketGoods);
        return BaseRespVo.ok();
    }

    @PostMapping("create")
    public BaseRespVo<Object> createGoods(@RequestBody GoodsAddBO addBO) {
        adminGoodsService.createGoods(addBO);
        return BaseRespVo.ok();
    }
    //注释

}
